<?php


_include(_OTEKOO_API_."APIDefault");
_include(_OTEKOO_MODEL_."Message");
_include(_OTEKOO_MODEL_."MessageMapper");
_include(_OTEKOO_MODEL_."User");
_include(_OTEKOO_MODEL_."UserMapper");

class APIGuest extends APIDefault
{
	private $_messageMapper = NULL;
	private $_userMapper = NULL;
	
	public function __construct(APIBootstrap $api){
		parent::__construct($api);
		$this->_messageMapper = new MessageMapper($this->_api->_db, $this->_api->_scripts);
		$this->_userMapper = new UserMapper($this->_api->_db, $this->_api->_scripts);
	}
	
	private function user(&$errors)
	{
		$item = $this->_userMapper->find($this->_request->token);
		if($item == null)
			$errors[] = "Missing user";
		return $item;
	}
	
	protected function friends(&$errors)
	{
		if(!isset($errors))
			$errors = array();
		$item = $this->_request->friends;
		$emails = explode(',', $item);
		$r = array();
		foreach($emails as $email)
		{
			$email = trim($email);
			$r[] = $email;
			if(!filter_var($email, FILTER_VALIDATE_EMAIL))
				$errors[] = "Invalid Email Address '$email'";
		}
		return $r;
	}
	
	private function talk_id(&$errors)
	{
		$item = $this->_request->talkid;
		if(!isset($item) || !is_numeric($item))
			$errors[] = "Missing Talk Id";
		return $item;
	}
	
	/** 
	* remove
	* @http_method post get put
	*/
	public function add()
	{
		$code = 202;
		$errors = array();
		try{
			$author = $this->user($errors);
			$emails = $this->friends($errors);
			$messageId = $this->talk_id($errors);
			if(empty($errors))
			{
				if($this->_messageMapper->hasAuthority($messageId, $author))
				{
					foreach($emails as $email)
					{
						$user = $this->_userMapper->findByEmail($email);
						if($user === null)
						{
							$user = new User(array('email'=>$email));
							$user = $this->_userMapper->save($user);
						}
						$this->_messageMapper->addGuest($messageId, $user);
					}
				}
			}
			else
				$errors[] = "Bad Author";
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	
	/**
	* remove
	* @http_method post get put
	*/
	public function remove()
	{
		$code = 202;
		$errors = array();
		try{
			$author = $this->user($errors);
			$emails = $this->friends($errors);
			$messageId = $this->talk_id($errors);
			if(empty($errors))
			{
				if($this->_messageMapper->hasAuthority($messageId, $author))
				{
					foreach($emails as $email)
					{
						$user = $this->_userMapper->findByEmail($email);
						if($user !== null)
							$this->_messageMapper->removeGuest($messageId, $user);
					}
				}
				else
					$errors[] = "Bad Author";
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
	/**
	* remove
	* @http_method post get put
	*/
	public function all()
	{
		$code = 202;
		$errors = array();
		try{
			$user = $this->user($errors);
			$messageId = $this->talk_id($errors);
			if(empty($errors))
			{
				if($this->_messageMapper->isGuest($messageId, $user))
					$this->_response->guests = $this->_messageMapper->listGuests($messageId, $user);
				else
					$errors[] = "Forbidden"
			}
		}catch(Exception $e)
		{
			$errors[] = $e->getMessage();
		}
		if(!empty($errors))
		{
			$code = 400;
			$this->_response->errors = $errors;
		}
		$this->response($code);
	}
}